﻿using System;
using System.Net;

namespace QDP.Platform.Admin.ViewModels
{
    public class WebClientOperater
    {
        public static void Write(string url, string content, Action<bool, string> Action)
        {
            WebClient client = new WebClient();
            client.UploadStringCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    Action(true, e.Result);
                }
                else
                {
                    Action(false, e.Error.Message);
                }
            };
            string baseUrl = client.BaseAddress.Remove(client.BaseAddress.ToLower().LastIndexOf("/clientbin/") + 1);
            client.UploadStringAsync(new Uri(baseUrl + url), content);
        }
        public static void Read(string url, Action<bool, string> Action)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    Action(true, e.Result);
                }
                else
                {
                    Action(false, e.Error.Message);
                }
            };
            string baseUrl = client.BaseAddress.Remove(client.BaseAddress.ToLower().LastIndexOf("/clientbin/") + 1);
            client.DownloadStringAsync(new Uri(baseUrl + url));
        }
    }
}
